//
//  LoginView.m
//  groupProject
//
//  Created by mac on 15/12/17.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "LoginView.h"
#import "Config.h"
#import "ReisgterView.h"
#import "PassLoginView.h"
#import "MyMessageBox.h"
#import "InfoViewController.h"
#import "AFHTTPRequestOperationManager.h"

#define ZM_SERVER_LOGIN @"http://115.28.55.133:8000/jsonlogin"
#define ZM_SERVER_REGISTER @"http://115.28.55.133:8000/jsonregister"
@interface LoginView ()<UITextFieldDelegate>

@end

@implementation LoginView
{
    UITextField *nameField;
    UITextField *passField;
    UIButton  * LogBtn;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor whiteColor];
    self.navigationItem.title = @"我的信息";
    
    if (self.LoginSwitchModel == YES) {
        UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc]initWithTitle:@"关闭"style:UIBarButtonItemStylePlain target:self action:@selector(backView)];
        [leftBtn setTintColor:[UIColor redColor]];
        self.navigationItem.leftBarButtonItem = leftBtn;
        
        UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithTitle:@"注册"style:UIBarButtonItemStylePlain target:self action:@selector(BtnLogin)];
        self.navigationItem.rightBarButtonItem = rightBtn;
    }
    [self addLabel];
    [self addTextField];
    [self AddButton];
    
    
}
-(void)addLabel{
    UILabel * label1 =[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*0.01, SCREEN_HEIGHT*0.2, SCREEN_WIDTH*0.2, SCREEN_WIDTH*0.1)];
    label1.text = @"账号";
    label1.textColor =[UIColor blackColor];
    [self.view addSubview:label1];
    UILabel * label2 =[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*0.01, SCREEN_HEIGHT*0.3, SCREEN_WIDTH*0.2, SCREEN_WIDTH*0.1)];
    label2.text = @"密码";
    label2.textColor =[UIColor blackColor];
    [self.view addSubview:label2];
    label1.textAlignment = NSTextAlignmentRight;
    label2.textAlignment = NSTextAlignmentRight;
}

-(void)addTextField{
    nameField = [[UITextField alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*0.25, SCREEN_HEIGHT*0.2, SCREEN_WIDTH/3.0*2, SCREEN_WIDTH*0.1)];
    nameField.borderStyle = UITextBorderStyleRoundedRect;
    
    nameField.keyboardType =  UIKeyboardTypeASCIICapable;
    nameField.delegate = self;
    [self.view addSubview:nameField];
    
    passField = [[UITextField alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*0.25, SCREEN_HEIGHT*0.3, SCREEN_WIDTH/3.0*2, SCREEN_WIDTH*0.1)];
    passField.borderStyle = UITextBorderStyleRoundedRect;
    //nameField.placeholder = @"请输入";
    passField.keyboardType =  UIKeyboardTypeASCIICapable;
    passField.delegate = self;
    [self.view addSubview:passField];
}
-(void)AddButton{
    
    LogBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*0.05, SCREEN_HEIGHT*0.4, SCREEN_WIDTH*0.9, SCREEN_WIDTH*0.1)];
    [LogBtn setTitle:@"确认登录" forState:UIControlStateNormal];
    [LogBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    LogBtn.backgroundColor = [UIColor redColor];
    [LogBtn.layer setMasksToBounds:YES];
    [LogBtn.layer setCornerRadius:10.0];//设置矩形四个圆角半径
    [LogBtn.layer setBorderWidth:1.0];//边框宽度
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 1, 0, 0, 1 });
    [LogBtn.layer setBorderColor:colorref];//边框颜色
    [self.view addSubview:LogBtn];
    [LogBtn addTarget:self action:@selector(LogBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)LogBtnClick{
    LogBtn.enabled= NO;
    AFHTTPRequestOperationManager *manager;
    manager = [AFHTTPRequestOperationManager manager];
    NSString *username = nameField.text;
    NSString *password = passField.text;
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:username,@"username",password,@"password",nil];
    [ manager POST:ZM_SERVER_LOGIN parameters:dic success:^(AFHTTPRequestOperation *operation,id response)
     {
         NSDictionary *dic = (NSDictionary *)response;
         NSNumber *code = [dic objectForKey:@"code"];
         NSNumber *level1 =[dic objectForKey:@"level"];
         if (code.intValue == 200)
         {
             [MyMessageBox showMessage:@"登陆成功！" duration:2];
             //将有效的 用户名 和密码 保存到 userdefault
             NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
             [userDefault setObject:username forKey:@"username"];
             [userDefault setObject:password forKey:@"password"];
             [userDefault setObject:level1 forKey:@"level1"];
             InfoViewController * PVC = [[InfoViewController alloc]init];
             [self.navigationController pushViewController:PVC animated:YES];
         }
         else
         {
             NSString *message = [dic objectForKey:@"msg"];
             [MyMessageBox showMessage:message duration:5];
         }
         
     } failure:^(AFHTTPRequestOperation *operation,NSError *error)
     {
         [MyMessageBox showMessage:@"客官，网络出错！" duration:2];
     }];
    
    
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    //收起键盘取消第一响应者
    [textField resignFirstResponder];
    return YES;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //收起键盘--点击边框外部
    [self.view endEditing:YES];
}
//返回登陆界面
-(void)backView{
    
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
}
//点击进入注册界面
-(void)BtnLogin{
    ReisgterView * vc2 = [[ReisgterView alloc]init];
    vc2.ReisgterSwitchModel = NO;
    [self.navigationController pushViewController:vc2 animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
